//
// Created by denglibin on 19-10-29.
//
/**
 *udp通信客户端
 **/

#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <ctype.h>
#include <strings.h>
#include <arpa/inet.h>
#include <string.h>

#define SERVER_PORT 8000
#define SERVER_IP "127.0.0.1"
static int  main_(){
    int sfd;
    char buf[1024];
    socklen_t  addr_len;
    struct sockaddr_in server_addr;
    int i, len;
    addr_len = sizeof(server_addr);

    sfd = socket(AF_INET, SOCK_DGRAM, 0);

    bzero(&server_addr, addr_len);
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(SERVER_PORT);
    inet_pton(AF_INET, SERVER_IP, &server_addr.sin_addr.s_addr);
    while (1) {
        fgets(buf, sizeof(buf), stdin);
        if(buf[0] == 'q'){
            exit(1);
        }
        sendto(sfd, buf, strlen(buf), 0, (struct sockaddr*)&server_addr, addr_len);
        bzero(buf, sizeof(buf));
        len = recvfrom(sfd, buf, sizeof(buf), 0, NULL, 0);
        buf[len] = '\0';
        printf("%s\n", buf);
    }

    return 0;
}

